package com.ldq.imooc.basics.multiThread;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/4/24 15:15
 * @Modified by :
 */
public class SynchronizedDemo {

    //共享变量
    private boolean ready = false;
    private int result = 0;
    private int number = 1;

    //写操作
    public synchronized void write() {
        ready = true;
        number = 2;
    }

    //读操作
    public synchronized void read() {
        if (ready) {
            result = number * 3;
        }
        System.out.println("result的值为：" + result);
    }

    //内部线程类
    private class ReadWriteThread extends Thread {

        //根据构造方法中传入的flag参数，确定线程执行读操作还是写操作
        private boolean flag;

        public ReadWriteThread(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {

            if (flag) {
                //构造方法中传入true，执行写操作
                write();
            } else {
                read();
            }
        }
    }

    public static void main(String[] args) {

        SynchronizedDemo demo = new SynchronizedDemo();
        //启动线程执行写操作
        demo.new ReadWriteThread(true).start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //启动线程执行读操作
        demo.new ReadWriteThread(false).start();
    }
}
